package com.lk.springboot.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

/**
 * @author lk
 * @date 2020/6/19 20:11
 * @description:
 */
public class UseMapApiUtil {
    private static final String BASEURL = "https://restapi.amap.com/v3/place/around?key=";
    private static final String KEY = "041c25e11b077bdb9b83742f1500f797";
    private static final String LOCATION = "&location=";
    private static final String KEYWORDS = "&keywords=%E5%9C%B0%E9%93%81%E7%AB%99";

    /**
     * 调用高德地图api，获取对应位置处的附近地铁站点信息
     *
     * @param j
     * @param w
     * @param num
     * @return
     */
    public static ArrayList<String> parseRequest(String j, String w, int num) {
        String path = BASEURL + KEY + LOCATION + j + "," + w + KEYWORDS;
        ArrayList<String> list = new ArrayList<>();
        try {
            URL url = new URL(path);
            //打开和url之间的连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");//GET和POST必须全大写
            conn.connect();
            //获取URLConnection对象对应的输入流
            InputStream is = conn.getInputStream();
            //构造一个字符流缓存
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String str = new String(br.readLine().getBytes(), "UTF-8");//解决中文乱码问题
            try {
                list = parseMapJson(str, num);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //关闭流
            is.close();
            //断开连接
            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }

    }

    /**
     * 解析json字符串，获取地铁站点id
     *
     * @param mapJsonData
     * @param num
     * @return
     * @throws JSONException
     */
    public static ArrayList<String> parseMapJson(String mapJsonData, int num) throws JSONException {
        JSONArray jsonArray = JSONObject.parseObject(mapJsonData).getJSONArray("pois");
        ArrayList<String> list = new ArrayList<>();
        if (num > jsonArray.size()) {
            num = jsonArray.size();
        }
        for (int i = 0; i < num; i++) {
            JSONObject jsonObject1 = (JSONObject) jsonArray.get(i);
            String stationId = (String) jsonObject1.get("id");//名称
            list.add(stationId);
        }
        return list;
    }

//    public static void main(String[] args) throws JSONException {
//        String j = "120.46371905426025";//经度
//        String w = "31.599285806784426";//纬度
//        int num = 4;
//        ArrayList<String> list = new ArrayList<>();
//        list = parseRequest(j,w,num);
//        for (String s : list) {
//            System.out.println(s);
//        }
//    }
}
